package com.D5Power.SceneTrans
{
	import com.D5Power.D5Game;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.utils.getTimer;
	
	public class MosaicTrans implements ISceneTrans
	{
		private var game:D5Game;
		private var mosaicSize:int;
		private var transIntervalMs:int;

		public function MosaicTrans(game:D5Game, mosaicSize:int = 64, transIntervalMs:int = 100)
		{
			this.game = game;
			this.mosaicSize = mosaicSize;
			this.transIntervalMs = transIntervalMs;
		}
		
		public function start(callBack:Function):void
		{
			var bd:BitmapData = new BitmapData(game.width, game.height);
			bd.draw(game);
			var time:uint = getTimer();
			var currentSize:int = mosaicSize;
			game.addEventListener(Event.ENTER_FRAME, function (e:Event):void 
			{
				if (getTimer() - time > transIntervalMs)
				{
					time = getTimer();
					if (currentSize < mosaicSize)
					{
						var o:Bitmap = game.getChildAt(game.numChildren - 1) as Bitmap;
						game.removeChildAt(game.numChildren - 1);
						o.bitmapData.dispose();
						if (currentSize == 1)
						{
							game.removeEventListener(Event.ENTER_FRAME, arguments.callee);
							callBack();
							return;
						}						
					}
					
					var pixBitMapData:BitmapData = new BitmapData(bd.width / currentSize, bd.height / currentSize, true, 0); 
					var theScaleMatrix:Matrix = new Matrix();
					theScaleMatrix.scale(1 / currentSize, 1 / currentSize);
					pixBitMapData.draw(bd, theScaleMatrix);
					
					var pixMap:Bitmap = new Bitmap(pixBitMapData);
					game.addChild(pixMap);
					pixMap.width = bd.width;
					pixMap.height = bd.height;
					if (currentSize == mosaicSize)
						game.visible = true;
					
					currentSize /= 2;
				}
			});
		}
		
		public function end(callBack:Function):void
		{
			var bd:BitmapData = new BitmapData(game.width, game.height);
			bd.draw(game);
			var time:uint = getTimer();
			var currentSize:int = 1;
			game.addEventListener(Event.ENTER_FRAME, function (e:Event):void 
			{
				if (getTimer() - time > transIntervalMs)
				{
					time = getTimer();
					if (currentSize > 1)
					{
						var o:Bitmap = game.getChildAt(game.numChildren - 1) as Bitmap;
						game.removeChildAt(game.numChildren - 1);
						o.bitmapData.dispose();
						if (currentSize == mosaicSize)
						{
							game.removeEventListener(Event.ENTER_FRAME, arguments.callee);
							callBack();
							return;
						}						
					}
					
					var pixBitMapData:BitmapData = new BitmapData(bd.width / currentSize, bd.height / currentSize, true, 0); 
					var theScaleMatrix:Matrix = new Matrix();
					theScaleMatrix.scale(1 / currentSize, 1 / currentSize);
					pixBitMapData.draw(bd, theScaleMatrix);
					
					var pixMap:Bitmap = new Bitmap(pixBitMapData);
					game.addChild(pixMap);
					pixMap.width = bd.width;
					pixMap.height = bd.height;
					
					currentSize *= 2;
				}
			});
		}		
	}
}